//
//  MyXMLParser.m
//  ZingBrowser
//
//  Created by -Xh- on 1/15/13.
//  Copyright (c) 2013 -Xh-. All rights reserved.
//

#import "MyXMLParser.h"

@implementation MyXMLParser{
    NSSet* interestingKeys;
}

@synthesize delegate;


-(id)init{
    self=[super init];
    if(self){
        interestingKeys=[[NSSet alloc]initWithObjects:@"suggestion", nil];
    }
    return self;
}

-(void)parseXMLData:(NSData *)_data{
    
    @try {
        resultArray=[[NSMutableArray alloc]init];
        xmlParser=[[NSXMLParser alloc]initWithData:_data];
        xmlParser.delegate=self;
        //NSString* dataString=[[NSString alloc]initWithData:_data encoding:NSUTF8StringEncoding];
       // NSLog(@"dataString = %@",dataString);
        [xmlParser parse];
    }
    @catch (NSException *exception) {
         NSLog(@"MyXMLParser>>Exception>>parseXMLData>>%@",[exception debugDescription]);
    }  
}



-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    
    @try {
        if([interestingKeys containsObject:elementName]){
            NSString* attributeData=(NSString*)[attributeDict objectForKey:@"data"];
            [resultArray addObject:attributeData];
        }    }
    @catch (NSException *exception) {
        NSLog(@"MyXMLParser>>Exception>>didStartElement>>%@",[exception debugDescription]);
    }
    
    
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    
    @try {
        if([elementName isEqualToString:@"toplevel"]){
            if(delegate && [delegate respondsToSelector:@selector(onParsingComplete:)]){
                [delegate onParsingComplete:resultArray];
            }
            return;
        }
    }
    @catch (NSException *exception) {
        NSLog(@"MyXMLParser>>Exception>>didEndElement>>%@",[exception debugDescription]);
    }
    
    
    
        
}




@end
